package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import qlsl.androiddesign.activity.commonactivity.MainActivity;
import qlsl.androiddesign.activity.subactivity.PostingDetailActivity;
import qlsl.androiddesign.activity.subactivity.SearchActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingDetailActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingListActivity;
import qlsl.androiddesign.adapter.subadapter.PostingAdapter;
import qlsl.androiddesign.adapter.subadapter.ShoppingListAdapter;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.Posting;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.http.service.commonservice.SearchService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase.Mode;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshListView;
import qlsl.androiddesign.listener.OnRefreshListener;
import qlsl.androiddesign.listener.OnTextChangeListener;
import qlsl.androiddesign.manager.ActivityManager;
import qlsl.androiddesign.util.commonutil.Log;
import qlsl.androiddesign.view.baseview.FunctionView;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.qlsl.androiddesign.kuaiwa.R;

/**
 * 搜索页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class SearchView extends FunctionView<SearchActivity> implements
		OnItemClickListener {

	private EditText et_search;

	private PullToRefreshListView refreshView;

	private List<Posting> list = new ArrayList<Posting>();

	private List<ShoppingDetail> list2 = new ArrayList<ShoppingDetail>();

	private Pager pager;

	public SearchView(SearchActivity activity) {
		super(activity);
		setContentView(R.layout.activity_mysearch);
	}

	protected void initView(View view) {
		hideTitleBar();
		et_search = findViewById(R.id.et_search);
		refreshView = findViewById(R.id.refreshListView);
		refreshView.setMode(Mode.PULL_FROM_END);
	}

	protected void initData() {

	}

	protected void initListener() {
		et_search.addTextChangedListener(textChangeListener);
		refreshView.setOnRefreshListener(refreshListener);
		refreshView.setOnItemClickListener(this);
	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<Posting> list_result = (List<Posting>) map.get("list");
			pager = (Pager) map.get("pager");
			if (pager.getPageNo() == 1) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged();
		} else if (t[0] instanceof List) {
			List<ShoppingDetail> list_result = (List<ShoppingDetail>) t[0];
			resetList2(list_result);
			notifyDataSetChanged2();
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_search:
			doClickSearchButton(view);
			break;

		case R.id.layout_group:
			doClickItemButton(view);
			break;
		case R.id.list_item:
			doClickItem(view);
			break;

		}
	}

	private void doClickItem(View view) {
		int position = refreshView.getRefreshableView()
				.getPositionForView(view);
		Posting item = (Posting) refreshView.getAdapter().getItem(position);
		startActivity(item, PostingDetailActivity.class);
	}

	private void doClickItemButton(View view) {
		int pos = refreshView.getRefreshableView().getPositionForView(view);
		ShoppingDetail item = (ShoppingDetail) refreshView.getAdapter()
				.getItem(pos);
		if (item != null) {
			startActivity(item, ShoppingDetailActivity.class);
		}
	}

	private void doClickSearchButton(View view) {
		Button btn_search = (Button) view;
		String text = btn_search.getText().toString();
		if (text.equals("取消")) {
			activity.finish();
		} else {
			search(0);
		}
	}

	private void search(int page) {
		String keyword = et_search.getText().toString();
		Log.checkDebugModel(activity, keyword);
		int currentItem = 1;
		Activity previousActivity = ActivityManager.getInstance()
				.previousActivity();
		if (previousActivity instanceof MainActivity) {
			currentItem = ((MainActivity) ActivityManager.getInstance()
					.previousActivity()).getCurrentItem();
		} else if (previousActivity instanceof ShoppingListActivity) {
			currentItem = 1;
		}

		if (currentItem == 1) {
			SearchService.searchShoppingName(1, keyword, this, activity);
		} else {
			SearchService.searchShoppingName2(1, keyword, this, activity);
		}
	}

	private void resetList(List<Posting> list_result) {
		list.clear();
		list.addAll(list_result);
		refreshView.getRefreshableView().smoothScrollToPositionFromTop(0, 0);
	}

	private void resetList2(List<ShoppingDetail> list_result) {
		list2.clear();
		list2.addAll(list_result);
		refreshView.getRefreshableView().smoothScrollToPositionFromTop(0, 0);
	}

	/**
	 * 帖子列表adapter
	 */
	private void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) refreshView.getBaseAdapter();
		if (adapter == null) {
			adapter = new PostingAdapter(activity, list);
			refreshView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		refreshView.onRefreshComplete();
	}

	/**
	 * 商城列表adapter
	 */
	private void notifyDataSetChanged2() {
		BaseAdapter adapter = (BaseAdapter) refreshView.getBaseAdapter();
		if (adapter == null) {
			adapter = new ShoppingListAdapter(activity, list2);
			refreshView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		refreshView.onRefreshComplete();
	}

	private OnTextChangeListener textChangeListener = new OnTextChangeListener() {

		public void afterTextChanged(android.text.Editable s) {
			Button btn_search = findViewById(R.id.btn_search);
			if (TextUtils.isEmpty(s)) {
				btn_search.setText("取消");
				btn_search.setActivated(false);
			} else {
				btn_search.setText("搜索");
				btn_search.setActivated(true);
			}
		};
	};

	public void onNothingSelected(AdapterView<?> parent) {

	}

	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

	}

	private OnRefreshListener<ListView> refreshListener = new OnRefreshListener<ListView>() {

		public void onRefresh() {
			if (pager != null) {
				super.onRefresh(refreshView, pager);
			} else {
				refreshView.onRefreshComplete();
			}
		}

		public void nextPager() {
			// search(pager.getPageNo() + 1);
		}
	};

}
